﻿using System;
using Microsoft.Phone.Controls;
using Coding4Fun.Toolkit.Controls;
using Coding4Fun.MissionControl.WP.Models;
using Microsoft.Phone.Shell;
using Coding4Fun.MissionControl.WP.ViewModels;

namespace Coding4Fun.MissionControl.WP.Views
{
    public partial class ServerListPage : PhoneApplicationPage
    {
        public ServerListPage()
        {
            InitializeComponent();
        }

        private void btnAddService_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/Views/AddServerPage.xaml", UriKind.Relative));
        }

        protected override void OnBackKeyPress(System.ComponentModel.CancelEventArgs e)
        {
            if (CommonViewModel.Instance.IsWaiting)
            {
                CommonViewModel.Instance.CommandClient.AbandonAttempt();

                CommonViewModel.Instance.IsWaiting = false;
                e.Cancel = true;
            }
            base.OnBackKeyPress(e);
        }


        private void btnAbout_Click(object sender, EventArgs e)
        {
            AboutPrompt aboutPrompt = new AboutPrompt();
            aboutPrompt.Show("Den Delimarsky - Coding4Fun\nShuttle icon by The Noun Project.", "@Coding4fun", "v-dedeli@microsoft.com", @"http://coding4fun.com");
        }

        private void SetAppBarButton(bool isServerView)
        {
            ApplicationBar.Buttons.Clear();

            var button = new ApplicationBarIconButton();
            button.IconUri = new Uri("/Graphics/appbar.add.png", UriKind.Relative);

            if (isServerView)
            {
                ApplicationBar.Mode = ApplicationBarMode.Default;

                button.Click += btnAddService_Click;
                button.Text = "add stack";
                ApplicationBar.Buttons.Add(button);
            }
            else
            {
                ApplicationBar.Mode = ApplicationBarMode.Minimized;
            }
        }

        private void Pivot_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if ((e.AddedItems[0] as PivotItem).Name == "ServersPivot")
            {
                SetAppBarButton(true);
            }
            else
            {
                SetAppBarButton(false);
            }
        }

        private async void QuickLaunch_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
            if (QuickLaunchList.SelectedItem != null)
            {
                Favorite favorite = QuickLaunchList.SelectedItem as Favorite;

                bool commandSent = await CommonViewModel.Instance.CommandClient.SendCommand(favorite.ServerLocation, 
                    string.Format(Constants.COMMAND_EXECUTE, favorite.ParentSet), favorite.CommandName);
            }
        }

        private void WaitingOverlayLayoutUpdated(object sender, EventArgs e)
        {
            if (grdConnectOverlay.Visibility == System.Windows.Visibility.Collapsed)
            {
                ApplicationBar.IsVisible = true;
            }
            else
            {
                ApplicationBar.IsVisible = false;
            }
        }
    }
}